class ArticleTagsController < ApplicationController
layout "page"
	def index
		@article = Article.find(params[:article_id])
#		@tags = @article.tag_counts(:order => 'name')
	end
	def show
		@article = Article.find(params[:article_id])
		@articles = @article.find_tagged_with(params[:id])

	end
	
  
  def remotetags
		@article = Article.find(params[:article_id])
  end
	
  def add_tag
    @article = Article.find(params[:id])
    @article.tag_list =@article.tag_list.to_s+ ',' + params[:tag][:name]
    @article.save
    @new_tag = @article.reload.tags.last
          if @new_tag.websitesinf_id.nil?
	     @new_tag.websitesinf_id = @article.websitesinf_id
	     @new_tag.save
	  end
		     @article.tag_id = @new_tag.id
		     @article.save
        
       if (Tag.find_by_name_and_websitesinf_id(params[:tag][:name],@article.websitesinf_id)).nil?
        @mytestobj = Tag.new
        @mytestobj.name = params[:tag][:name]
        @mytestobj.websitesinf_id = @article.websitesinf_id
        @mytestobj.save
        
     
		     @article.tag_id = @mytestobj.id
		     @article.save
       end 
  end
  
  def remove_tag
    @article = Article.find(params[:id])
    @tag_to_delete = @article.tags.find(params[:tag_id])
    if @tag_to_delete
   (Tag.find_by_name_and_websitesinf_id(@tag_to_delete.name,@article.websitesinf_id)).destroy
      @article.tags.delete(@tag_to_delete)
    else
      render :nothing => true
    end
  end
  
	def addt
  	tagname=params[:tagname].to_s.strip
		unless tagname.blank?
			@tag=Tag.find_by_name(params[:tagname])
			if @tag.nil?
	          @tag = Tag.new()
	          @tag.name=params[:tagname]
	          @tag.websitesinf_id = (Websitesinf.find_by_iscurrent(true)).id
			  @tag.save
		    else
		      
		    end
		end
  end   

end
